package com.example.renyang.happysell;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.renyang.happysell.MyUntil.Data;
import com.example.renyang.happysell.net.HttpUtil;

public class LoginActivity extends AppCompatActivity {
    private Button bt=null;
    EditText unameET=null;
    EditText passwordET=null;
  String uname=null;
   String password=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
       TextView tv=(TextView)findViewById(R.id.registerTV);
       unameET =(EditText)findViewById(R.id.usernameEt);
        passwordET=(EditText)findViewById(R.id.passwordET);

        bt=(Button)findViewById(R.id.login_Btn);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uname=unameET.getText().toString();
                password=passwordET.getText().toString();
                Log.i("zx",password+"ddddddddd"+uname);
                login(uname, password);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void login(String name,String id){

                try {
                    Log.i("zx", uname + id + "name>>>>>>>>>>>>>>>");
                    String s = query0(name, id);
                    Log.i("zx", s + "登录成功");
                    Data.setName(uname);
                    Intent intent=new Intent();
                    intent.setClass(LoginActivity.this,fragment_mall.class);
                    startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                    Log.i("zx","登录失败");
                }

    }

    private String query0(String name,String id) throws  Exception{
        String url= HttpUtil.BASE_URL+"userLogin?uname="+name+"&upassword="+id;
        String ss=HttpUtil.getRequest(url);
        return null;
    }

}
